package br.com.academia.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.academia.dao.UsuarioDAO;
import br.com.academia.modelo.Usuario;

public class UsuarioAlterar extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public UsuarioAlterar() {
		super();
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		int id = Integer.parseInt(request.getParameter("id"));
		String login = request.getParameter("login").trim();
		String senha = request.getParameter("senha").trim();
		String email = request.getParameter("email").trim();

		if (login.equals("") || senha.equals("") || email.equals("")) {
			out.print("<script language='javascript'>");
			out.print("alert('Preencha todos os campos!');");
			out.print("history.back();");
			out.print("</script>");
			return;
		}

		Usuario usuario = new Usuario();
		usuario.setIdusuario(id);
		usuario.setLogin(login);
		usuario.setSenha(senha);
		usuario.setEmail(email);

		UsuarioDAO dao = new UsuarioDAO();

		out.print("<script language='javascript'>");
		if (dao.alterar(usuario)) {
			out.print("alert('Informacao Salva!');");
		}else{
			out.print("alert('Erro!');");
		}
		out.print("location.href='/academia/menu/usuario-listar.jsp'");
		out.print("</script>");
	}

}
